﻿Imports AjaxControlToolkit
Imports System.IO


Public Class AdvertiseDashboard
    Inherits System.Web.UI.Page
    Public numAcc As Integer
    Public numAcc2 As Integer
    Public dbManager As DBManager = New DBManager()

    'To set masterpage 
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit

        'Check if user is null
        If (Session("user") IsNot Nothing) Then
            'Retrieve userAcc details from session
            Dim userLoggedIn As userAcc = Session("user")
            'Check if user is admin, assign masterpage respectively
            If userLoggedIn.isAdmin Then
                Me.MasterPageFile = "~/MasterPage/Admin.Master"
            Else
                Me.MasterPageFile = "~/MasterPage/User.Master"
            End If
        Else

            'For anonymous users
            Me.MasterPageFile = "~/MasterPage/User.Master"
            'Enable this line to prevent anonymous users from entering this page
            Response.Redirect("~/Home.aspx")
        End If

    End Sub

    'Page load event
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Session("user") IsNot Nothing) Then
            Dim userLoggedIn As userAcc = Session("user")

            Dim objdatasource As SqlDataSource = SqlDataSourceSEOPayment
            Dim parameter As Parameter = objdatasource.SelectParameters(0)
            parameter.DefaultValue = userLoggedIn.userID
            lvConfirmSEO.DataSourceID = objdatasource.ID
            lvConfirmSEO.DataBind()

            Dim objdatasource2 As SqlDataSource = SqlDataSourceBanner
            parameter = objdatasource2.SelectParameters(0)
            parameter.DefaultValue = userLoggedIn.userID
            lvConfirmBanner.DataSourceID = objdatasource2.ID
            lvConfirmBanner.DataBind()
        Else
        End If


    End Sub

    'Make necessary changes depending on whether the item is live or payment pending
    Protected Sub lvConfirmBanner_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles lvConfirmBanner.ItemDataBound
        Dim lblStatus As Label = e.Item.FindControl("lblStatus")
        If lblStatus.Text = "initialized" Or lblStatus.Text = "initializing" Then
            e.Item.Visible = False
        Else
            Dim lblImageName As Label = e.Item.FindControl("lblImageName")
            Dim lblStartDate As Label = e.Item.FindControl("lblStartDate")
            Dim lblCost As Label = e.Item.FindControl("lblCost")
            Dim lblCostOnly As Label = e.Item.FindControl("lblCostOnly")
            Dim lblID As Label = e.Item.FindControl("lblID")
            Dim lblEndDate As Label = e.Item.FindControl("lblEndDate")
            Dim lblClicks As Label = e.Item.FindControl("lblClicks")
            Dim lblImpressions As Label = e.Item.FindControl("lblImpressions")
            Dim nameLabel As Label = e.Item.FindControl("accBanner").FindControl("accBannerPane").FindControl("nameLabel")
            Dim start_dateLabel As Label = e.Item.FindControl("accBanner").FindControl("accBannerPane").FindControl("start_dateLabel")
            Dim costLabel As Label = e.Item.FindControl("accBanner").FindControl("accBannerPane").FindControl("costLabel")
            Dim statusLabel As Label = e.Item.FindControl("accBanner").FindControl("accBannerPane").FindControl("statusLabel")
            Dim lblEndDateBanner As Label = e.Item.FindControl("accBanner").FindControl("accBannerPane").FindControl("lblEndDateBanner")
            Dim lblCostBanner As Label = e.Item.FindControl("accBanner").FindControl("accBannerPane").FindControl("lblCostBanner")
            Dim imgPreview As Image = e.Item.FindControl("accBanner").FindControl("accBannerPane").FindControl("imgPreview")

            Dim lblStartDateEnabled As Label = e.Item.FindControl("accBanner").FindControl("accBannerPane").FindControl("lblStartDateEnabled")

            Dim lblEndDateEnabled As Label = e.Item.FindControl("accBanner").FindControl("accBannerPane").FindControl("lblEndDateEnabled")

            nameLabel.Text = lblImageName.Text
            start_dateLabel.Text = "<b>Start Date : </b>" + lblStartDate.Text
            costLabel.Text = lblCost.Text
            statusLabel.Text = lblStatus.Text
            imgPreview.ImageUrl = "BannerHttpHandler.ashx?id=" + lblID.Text
            imgPreview.Visible = True
            lblEndDateBanner.Text = lblEndDate.Text
            lblCostBanner.Text = 100

            'Try
            '    Dim tbxDateBanner As TextBox = e.Item.FindControl("accBanner").FindControl("accBannerPane").FindControl("contentPanel").Controls(1)
            '    tbxDateBanner.Text = start_dateLabel.Text
            'Catch ex As Exception
            Dim tbxDateBanner As TextBox = e.Item.FindControl("accBanner").FindControl("accBannerPane").FindControl("contentPanel").Controls(0)
            tbxDateBanner.Text = lblStartDate.Text
            'End Try
            If statusLabel.Text = "live" Then
                Dim pnlEnabled As Panel = e.Item.FindControl("accBanner").FindControl("accBannerPane").FindControl("pnlEnabled")
                Dim pnlInit As Panel = e.Item.FindControl("accBanner").FindControl("accBannerPane").FindControl("pnlInit")
                Dim divHeader As HtmlGenericControl = e.Item.FindControl("accBanner").FindControl("accBannerPane").FindControl("divHeader")
                Dim clicksLabel As Label = e.Item.FindControl("accBanner").FindControl("accBannerPane").FindControl("clicksLabel")
                Dim impressionsLabel As Label = e.Item.FindControl("accBanner").FindControl("accBannerPane").FindControl("impressionsLabel")
                clicksLabel.Text = "<b>Clicks : </b>" + lblClicks.Text
                impressionsLabel.Text = "<b>Impressions : </b>" + lblImpressions.Text
                start_dateLabel.Visible = False
                costLabel.Visible = False
                clicksLabel.Visible = True
                impressionsLabel.Visible = True
                pnlInit.Visible = False
                pnlEnabled.Visible = True
                lblStartDateEnabled.Text = "<b>Start Date : </b>" + lblStartDate.Text
                lblEndDateEnabled.Text = lblEndDate.Text
                divHeader.Style("background-color") = "#c7edd5"
            End If
        End If
    End Sub

    'Handles the commands for the listview
    Protected Sub lvConfirmBanner_ItemCommand(ByVal sender As Object, ByVal e As AccordionCommandEventArgs)
        Dim panelView As Panel = e.Container.FindControl("pnlEditNo")
        Dim panelEdit As Panel = e.Container.FindControl("pnlEditYes")
        Dim lbCancelBanner As LinkButton = e.Container.FindControl("lbCancelBanner")
        Dim lbDeleteBanner As LinkButton = e.Container.FindControl("lbDeleteBanner")
        Dim lbEditBanner As LinkButton = e.Container.FindControl("lbEditBanner")
        Dim fileUpload As FileUpload = e.Container.FindControl("FileUpload1")
        Dim lblImgUploaded As Label = e.Container.FindControl("lblImgUploaded")
        Dim lbConfirmEditBanner As LinkButton = e.Container.FindControl("lbConfirmEditBanner")
        Dim lblID As Label = e.Container.Parent.Parent.FindControl("lblID")
        Dim tbxStartDate As TextBox = e.Container.FindControl("contentPanel").Controls(0)
        Dim lblEndDateBanner As Label = e.Container.FindControl("lblEndDateBanner")
        Dim lblCostBanner As Label = e.Container.FindControl("lblCostBanner")
        Dim lblOutletID As Label = e.Container.Parent.Parent.FindControl("lblOutletID")

        Select Case e.CommandName
            Case "deleteBanner"
                dbManager.deleteImageBanner(Convert.ToInt32(lblID.Text))
                lvConfirmBanner.DataBind()
            Case "editBanner"
                panelView.Visible = False
                panelEdit.Visible = True
                lbCancelBanner.Visible = True
                lbDeleteBanner.Visible = False
                lbEditBanner.Visible = False
                lblImgUploaded.Visible = False

            Case "cancelEdit"
                panelView.Visible = True
                panelEdit.Visible = False
                lbCancelBanner.Visible = False
                lbDeleteBanner.Visible = True
                lbEditBanner.Visible = True
                lblImgUploaded.Visible = False

            Case "upload"
                If fileUpload.HasFile Then
                    lblImgUploaded.Visible = True
                    Dim imgLength As Integer = fileUpload.PostedFile.ContentLength
                    Dim img As HttpPostedFile = fileUpload.PostedFile
                    Dim imgName As String = Path.GetFileName(fileUpload.PostedFile.FileName)
                    Dim userLoggedIn As userAcc = Session("user")
                    If Not e.CommandArgument = "" Then
                        dbManager.deleteImageBanner(Convert.ToInt32(e.CommandArgument))
                    End If
                    lbConfirmEditBanner.CommandArgument = dbManager.updateBannerImageTemp(imgName, img, imgLength, 3)
                End If
            Case "confirmEdit"
                Dim tempID = e.CommandArgument
                dbManager.updateImageBanner(Convert.ToInt32(lblID.Text), tbxStartDate.Text, lblEndDateBanner.Text, Convert.ToInt32(lblCostBanner.Text), Convert.ToInt32(lblOutletID.Text))
                dbManager.updateImageBannerUnconfirmed(Convert.ToInt32(lblID.Text))

                If Not tempID = "" Then
                    dbManager.updateBannerImageChange(Convert.ToInt32(lblID.Text), tempID)
                End If
                panelView.Visible = True
                panelEdit.Visible = False
                lbCancelBanner.Visible = False
                lbDeleteBanner.Visible = True
                lbEditBanner.Visible = True
                lblImgUploaded.Visible = False
                lvConfirmBanner.DataBind()
        End Select
    End Sub

    'Dynamically creates controls to prevent duplicate IDs
    Protected Sub accSEOCreateControls(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim accordionPane As AccordionPane = sender.FindControl("accSEOPane")

        'Dynamically creating Delete button
        Dim lbDeleteSEO As LinkButton = New LinkButton()
        lbDeleteSEO.CommandName = "deleteSEO"
        lbDeleteSEO.CssClass = "close"
        lbDeleteSEO.Text = "Delete"
        lbDeleteSEO.Style.Add("font-size", "16px !important")
        lbDeleteSEO.ID = "lbDeleteSEO" + numAcc2.ToString()
        lbDeleteSEO.CausesValidation = False
        accordionPane.ContentContainer.FindControl("phDeleteSEO").Controls.Add(lbDeleteSEO)

        'Dynamically create ConfirmButtonExtender
        Dim cbeDelete As ConfirmButtonExtender = New ConfirmButtonExtender()
        cbeDelete.ConfirmText = "Are you sure you want to delete this banner ? "
        cbeDelete.TargetControlID = lbDeleteSEO.ID
        cbeDelete.ID = "cbeDeleteSEO" + numAcc2.ToString()
        accordionPane.ContentContainer.FindControl("phDeleteSEO").Controls.Add(cbeDelete)

        'Dynamically create textbox
        Dim tbxDatePicker As TextBox = New TextBox()
        tbxDatePicker.Enabled = False
        tbxDatePicker.ID = "tbxDatePicker" + numAcc2.ToString()
        tbxDatePicker.CausesValidation = True
        tbxDatePicker.AutoPostBack = True
        tbxDatePicker.Width = New Unit(90, UnitType.Pixel)
        AddHandler (tbxDatePicker.TextChanged), AddressOf tbxDateSEO_textChanged
        accordionPane.ContentContainer.FindControl("phStartDateSEO").Controls.Add(tbxDatePicker)

        'Dynamically create range validator
        Dim rvDatePicker As RangeValidator = New RangeValidator
        rvDatePicker.MinimumValue = System.DateTime.Now.AddDays(7).Date
        rvDatePicker.MaximumValue = System.DateTime.Now.AddDays(7).AddYears(1).Date
        rvDatePicker.ErrorMessage = "Must be at least 7 days in advance and within a year"
        rvDatePicker.Type = ValidationDataType.Date
        rvDatePicker.ID = "rvDatePicker" + numAcc2.ToString()
        rvDatePicker.Display = ValidatorDisplay.Dynamic
        rvDatePicker.ControlToValidate = tbxDatePicker.ID
        rvDatePicker.Enabled = False
        accordionPane.ContentContainer.FindControl("phStartDateSEO").Controls.Add(rvDatePicker)

        'Dynamically create calenderExtender
        Dim calExDate As CalendarExtender = New CalendarExtender()
        calExDate.TargetControlID = tbxDatePicker.ID
        calExDate.Enabled = True
        calExDate.Format = "dd/MM/yyyy"
        calExDate.PopupPosition = CalendarPosition.Right
        calExDate.ID = "calExDate" + numAcc2.ToString()
        accordionPane.ContentContainer.FindControl("phStartDateSEO").Controls.Add(calExDate)

        'Dynamically create ValidatorCallOutExtender
        Dim valCallEx As ValidatorCalloutExtender = New ValidatorCalloutExtender()
        valCallEx.PopupPosition = ValidatorCalloutPosition.Right
        valCallEx.TargetControlID = rvDatePicker.ID
        valCallEx.ID = "valCallEx" + numAcc2.ToString()
        accordionPane.ContentContainer.FindControl("phStartDateSEO").Controls.Add(valCallEx)

        numAcc2 = numAcc2 + 1
    End Sub
    Protected Sub accBannerCreateControls(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim accordionPane As AccordionPane = sender.FindControl("accBannerPane")
        'Dynamically create textbox
        Dim tbxDatePicker As TextBox = New TextBox()
        tbxDatePicker.ID = "tbxDatePicker" + numAcc.ToString()
        tbxDatePicker.CausesValidation = True
        tbxDatePicker.AutoPostBack = True
        tbxDatePicker.Width = New Unit(90, UnitType.Pixel)
        AddHandler (tbxDatePicker.TextChanged), AddressOf tbxDateBanner_textChanged
        accordionPane.ContentContainer.FindControl("contentPanel").Controls.Add(tbxDatePicker)

        'Dynamically create range validator
        Dim rvDatePicker As RangeValidator = New RangeValidator
        rvDatePicker.MinimumValue = System.DateTime.Now.AddDays(7).Date
        rvDatePicker.MaximumValue = System.DateTime.Now.AddDays(7).AddYears(1).Date
        rvDatePicker.ErrorMessage = "Must be at least 7 days in advance and within a year"
        rvDatePicker.Type = ValidationDataType.Date
        rvDatePicker.ID = "rvDatePicker" + numAcc.ToString()
        rvDatePicker.Display = ValidatorDisplay.Dynamic
        rvDatePicker.ControlToValidate = tbxDatePicker.ID
        'rvDatePicker.ForeColor = Drawing.Color.FromArgb(251, 238, 213)
        rvDatePicker.Display = ValidatorDisplay.None
        accordionPane.ContentContainer.FindControl("contentPanel").Controls.Add(rvDatePicker)

        'Dynamically create calenderExtender
        Dim calExDate As CalendarExtender = New CalendarExtender()
        calExDate.TargetControlID = tbxDatePicker.ID
        calExDate.Enabled = True
        calExDate.Format = "dd/MM/yyyy"
        calExDate.PopupPosition = CalendarPosition.Right
        calExDate.ID = "calExDate" + numAcc.ToString()
        accordionPane.ContentContainer.FindControl("contentPanel").Controls.Add(calExDate)

        'Dynamically create ValidatorCallOutExtender
        Dim valCallEx As ValidatorCalloutExtender = New ValidatorCalloutExtender()
        valCallEx.PopupPosition = ValidatorCalloutPosition.Right
        valCallEx.TargetControlID = rvDatePicker.ID
        valCallEx.ID = "valCallEx" + numAcc.ToString()
        accordionPane.ContentContainer.FindControl("contentPanel").Controls.Add(valCallEx)

        'Dynamically create ConfirmButtonExtender
        Dim cbeDelete As ConfirmButtonExtender = New ConfirmButtonExtender()
        cbeDelete.ConfirmText = "Are you sure you want to delete this banner ? "
        cbeDelete.TargetControlID = "lbDeleteBanner"
        cbeDelete.ID = "cbeDelete" + numAcc.ToString()
        accordionPane.ContentContainer.FindControl("phDeleteCbe").Controls.Add(cbeDelete)

        numAcc = numAcc + 1
    End Sub

    'On text change 
    Protected Sub tbxDateSEO_textChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ddlSEODuration As DropDownList = sender.parent.findcontrol("ddlSEODuration")
        Dim lblCostSEOEdit As Label = sender.parent.findcontrol("lblCostSEOEdit")
        Dim selectedDate As Date = DateTime.Parse(sender.Text)
        Dim endDate As Date
        Select Case ddlSEODuration.SelectedIndex
            Case 0
                endDate = selectedDate.Date.AddMonths(1)
                lblCostSEOEdit.Text = "100"
            Case 1
                endDate = selectedDate.Date.AddMonths(3)
                lblCostSEOEdit.Text = "275"
            Case 2
                endDate = selectedDate.Date.AddMonths(6)
                lblCostSEOEdit.Text = "500"
            Case 3
                endDate = selectedDate.Date.AddYears(1)
                lblCostSEOEdit.Text = "900"
        End Select
        Dim lblEndDateSEOEdit As Label = sender.parent.findcontrol("lblEndDateSEOEdit")
        lblEndDateSEOEdit.Text = endDate
    End Sub

    'On text change
    Protected Sub tbxDateBanner_textChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ddlAccDuration As DropDownList = sender.parent.findcontrol("ddlAccDuration")
        Dim lblCostBanner As Label = sender.parent.findcontrol("lblCostBanner")
        Dim selectedDate As Date = DateTime.Parse(sender.Text)
        Dim endDate As Date
        Select Case ddlAccDuration.SelectedIndex
            Case 0
                endDate = selectedDate.Date.AddMonths(1)
                lblCostBanner.Text = "400"
            Case 1
                endDate = selectedDate.Date.AddMonths(3)
                lblCostBanner.Text = "1000"
            Case 2
                endDate = selectedDate.Date.AddMonths(6)
                lblCostBanner.Text = "1800"
            Case 3
                endDate = selectedDate.Date.AddYears(1)
                lblCostBanner.Text = "3300"
        End Select
        Dim lblEndDate As Label = sender.parent.findcontrol("lblEndDateBanner")
        lblEndDate.Text = endDate
    End Sub

    'On selected index change
    Protected Sub ddlDurationSEOChange(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ddlSEODuration As DropDownList = sender
        Dim lblCostSEOEdit As Label = sender.parent.findcontrol("lblCostSEOEdit")
        Dim selectedDate As Date = DateTime.Parse(sender.parent.findcontrol("phStartDateSEO").controls(0).text)
        Dim endDate As Date
        Select Case ddlSEODuration.SelectedIndex
            Case 0
                endDate = selectedDate.Date.AddMonths(1)
                lblCostSEOEdit.Text = "100"
            Case 1
                endDate = selectedDate.Date.AddMonths(3)
                lblCostSEOEdit.Text = "275"
            Case 2
                endDate = selectedDate.Date.AddMonths(6)
                lblCostSEOEdit.Text = "500"
            Case 3
                endDate = selectedDate.Date.AddYears(1)
                lblCostSEOEdit.Text = "900"
        End Select
        Dim lblEndDateSEOEdit As Label = sender.parent.findcontrol("lblEndDateSEOEdit")
        lblEndDateSEOEdit.Text = endDate
    End Sub

    'On selected index change
    Protected Sub ddlDurationBannerChange(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ddlAccDuration As DropDownList = sender
        Dim lblCostBanner As Label = sender.parent.findcontrol("lblCostBanner")
        Dim selectedDate As Date = DateTime.Parse(sender.parent.findcontrol("contentPanel").controls(1).text)
        Dim endDate As Date
        Select Case ddlAccDuration.SelectedIndex
            Case 0
                endDate = selectedDate.Date.AddMonths(1)
                lblCostBanner.Text = "400"
            Case 1
                endDate = selectedDate.Date.AddMonths(3)
                lblCostBanner.Text = "1000"
            Case 2
                endDate = selectedDate.Date.AddMonths(6)
                lblCostBanner.Text = "1800"
            Case 3
                endDate = selectedDate.Date.AddYears(1)
                lblCostBanner.Text = "3300"
        End Select
        Dim lblEndDate As Label = sender.parent.findcontrol("lblEndDateBanner")
        lblEndDate.Text = endDate
    End Sub

    'Make necessary changes depending on whether the item is live or payment pending
    Protected Sub lvConfirmSEO_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles lvConfirmSEO.ItemDataBound
        Dim lblImageName As Label = e.Item.FindControl("lblName")
        Dim lblStartDate As Label = e.Item.FindControl("lblStartDate")
        Dim lblDuration As Label = e.Item.FindControl("lblDuration")
        Dim lblStatus As Label = e.Item.FindControl("lblStatus")
        Dim lblID As Label = e.Item.FindControl("lblID")
        Dim lblOutletID As Label = e.Item.FindControl("lblOutletID")
        Dim lblEndDate As Label = e.Item.FindControl("lblEndDate")
        Dim lblClicks As Label = e.Item.FindControl("lblClicks")
        Dim lblImpressions As Label = e.Item.FindControl("lblImpressionsSeoHidden")

        Dim nameLabel As Label = e.Item.FindControl("accSEO").FindControl("accSEOPane").FindControl("seoNameLabel")
        Dim start_dateLabel As Label = e.Item.FindControl("accSEO").FindControl("accSEOPane").FindControl("seoStart_dateLabel")
        Dim durationLabel As Label = e.Item.FindControl("accSEO").FindControl("accSEOPane").FindControl("durationLabelSEO")
        Dim costLabel As Label = e.Item.FindControl("accSEO").FindControl("accSEOPane").FindControl("lblCost")
        Dim statusLabel As Label = e.Item.FindControl("accSEO").FindControl("accSEOPane").FindControl("seoStatusLabel")
        Dim ddlSEODuration As DropDownList = e.Item.FindControl("accSEO").FindControl("accSEOPane").FindControl("ddlSEODuration")
        Dim hlSEOLink As HyperLink = e.Item.FindControl("accSEO").FindControl("accSEOPane").FindControl("hlSEOLink")
        Dim lblEndDateSEOEdit As Label = e.Item.FindControl("accSEO").FindControl("accSEOPane").FindControl("lblEndDateSEOEdit")
        Dim lblCostSEOEdit As Label = e.Item.FindControl("accSEO").FindControl("accSEOPane").FindControl("lblCostSEOEdit")
        Dim lblImpressionsSEO As Label = e.Item.FindControl("accSEO").FindControl("accSEOPane").FindControl("lblImpressionsSEO")
        Dim lblClicksSEO As Label = e.Item.FindControl("accSEO").FindControl("accSEOPane").FindControl("lblClicksSEO")
        Dim tbxDateSEO As TextBox = e.Item.FindControl("accSEO").FindControl("accSEOPane").FindControl("phStartDateSEO").Controls(0)
        hlSEOLink.NavigateUrl = "OutletReview.aspx?id=" + lblOutletID.Text
        hlSEOLink.Text = lblImageName.Text
        Select Case (lblDuration.Text)
            Case "<b>Duration : </b>1 Month"
                ddlSEODuration.SelectedIndex = 0
                costLabel.Text = "<b>Cost : </b>$100"
                lblCostSEOEdit.Text = "100"
            Case "<b>Duration : </b>3 Months"
                ddlSEODuration.SelectedIndex = 1
                costLabel.Text = "<b>Cost : </b>$275"
                lblCostSEOEdit.Text = "275"
            Case "<b>Duration : </b>6 Months"
                ddlSEODuration.SelectedIndex = 2
                costLabel.Text = "<b>Cost : </b>$500"
                lblCostSEOEdit.Text = "500"
            Case "<b>Duration : </b>1 Year"
                ddlSEODuration.SelectedIndex = 3
                costLabel.Text = "<b>Cost : </b>$900"
                lblCostSEOEdit.Text = "900"
        End Select
        nameLabel.Text = lblImageName.Text
        start_dateLabel.Text = "<b>Start Date : </b>" + lblStartDate.Text
        statusLabel.Text = lblStatus.Text
        lblEndDateSEOEdit.Text = lblEndDate.Text
        tbxDateSEO.Text = lblStartDate.Text
        If statusLabel.Text = "live" Then
            lblClicksSEO.Text = "<b>Clicks : </b>" + lblClicks.Text
            lblClicksSEO.Visible = True
            lblImpressionsSEO.Text = "<b>Impressions : </b>" + lblImpressions.Text
            lblImpressionsSEO.Visible = True
            start_dateLabel.Visible = False
            costLabel.Visible = False
            Dim divSEOHeader As HtmlGenericControl = e.Item.FindControl("accSEO").FindControl("accSEOPane").FindControl("divSEOHeader")
            Dim lbCancelSEO As LinkButton = e.Item.FindControl("accSEO").FindControl("accSEOPane").FindControl("lbCancelSEO")
            Dim lbDeleteSEO As LinkButton = e.Item.FindControl("accSEO").FindControl("accSEOPane").FindControl("phDeleteSEO").Controls(0)
            Dim lbEditSEO As LinkButton = e.Item.FindControl("accSEO").FindControl("accSEOPane").FindControl("lbEditSEO")
            lbCancelSEO.Visible = False
            lbDeleteSEO.Visible = False
            lbEditSEO.Visible = False
            divSEOHeader.Style("background-color") = "#c7edd5"
        End If
    End Sub

    'Handles item command for listview
    Protected Sub lvConfirmSEO_ItemCommand(ByVal sender As Object, ByVal e As AccordionCommandEventArgs)
        Dim ddlSEODuration As DropDownList = e.Container.FindControl("ddlSEODuration")
        Dim tbxDateSEO As TextBox = e.Container.FindControl("phStartDateSEO").Controls(0)
        Dim rvDateSEO As RangeValidator = e.Container.FindControl("phStartDateSEO").Controls(1)
        Dim lbCancelSEO As LinkButton = e.Container.FindControl("lbCancelSEO")
        Dim lbDeleteSEO As LinkButton = e.Container.FindControl("phDeleteSEO").Controls(0)
        Dim lbEditSEO As LinkButton = e.Container.FindControl("lbEditSEO")
        Dim lblEndDateSEOEdit As Label = e.Container.FindControl("lblEndDateSEOEdit")
        Dim lblCostSEOEdit As Label = e.Container.FindControl("lblCostSEOEdit")
        Dim lbConfirmEditSEO As LinkButton = e.Container.FindControl("lbConfirmEditSEO")

        Dim lblStartDate As Label = e.Container.Parent.Parent.FindControl("lblStartDate")
        Dim lblEndDate As Label = e.Container.Parent.Parent.FindControl("lblEndDate")
        Dim lblDuration As Label = e.Container.Parent.Parent.FindControl("lbDurationOnly")
        Dim lblID As Label = e.Container.Parent.Parent.FindControl("lblID")
        Select Case e.CommandName
            Case "editSEO"

                ddlSEODuration.Enabled = True
                tbxDateSEO.Enabled = True
                rvDateSEO.Enabled = True

                lbCancelSEO.Visible = True
                lbDeleteSEO.Visible = False
                lbEditSEO.Visible = False
                lbConfirmEditSEO.Visible = True
            Case "cancelEdit"
                lbConfirmEditSEO.Visible = False
                ddlSEODuration.Enabled = False
                tbxDateSEO.Enabled = False
                rvDateSEO.Enabled = False

                lbCancelSEO.Visible = False
                lbDeleteSEO.Visible = True
                lbEditSEO.Visible = True
                tbxDateSEO.Text = lblStartDate.Text
                lblEndDateSEOEdit.Text = lblEndDate.Text

                Select Case (lblDuration.Text)
                    Case "1 Month"
                        ddlSEODuration.SelectedIndex = 0
                        lblCostSEOEdit.Text = "400"
                    Case "3 Months"
                        ddlSEODuration.SelectedIndex = 1
                        lblCostSEOEdit.Text = "1000"
                    Case "6 Months"
                        ddlSEODuration.SelectedIndex = 2
                        lblCostSEOEdit.Text = "1800"
                    Case "1 Year"
                        ddlSEODuration.SelectedIndex = 3
                        lblCostSEOEdit.Text = "3300"
                End Select
            Case "confirmEdit"
                Dim duration = ddlSEODuration.SelectedItem.Text
                Dim startDate = tbxDateSEO.Text
                Dim endDate = lblEndDateSEOEdit.Text
                dbManager.updateSEOItem(Convert.ToInt32(lblID.Text), duration, DateTime.Parse(startDate), DateTime.Parse(endDate))
                lbConfirmEditSEO.Visible = False
                ddlSEODuration.Enabled = False
                tbxDateSEO.Enabled = False
                rvDateSEO.Enabled = False

                lbCancelSEO.Visible = False
                lbDeleteSEO.Visible = True
                lbEditSEO.Visible = True
                lvConfirmSEO.DataBind()
            Case "deleteSEO"
                dbManager.deleteSEOItem(Convert.ToInt32(lblID.Text))
                lvConfirmSEO.DataBind()
        End Select
    End Sub

    'Proceed to payment page
    Protected Sub btnMakePayment_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnMakePayment.Click
        If (Session("user") IsNot Nothing) Then
            Dim userLoggedIn As userAcc = Session("user")

            'Clear payments
            dbManager.deletePaymentItems(userLoggedIn.userID)

            For Each item As ListViewItem In lvConfirmSEO.Items
                Dim lblStatus As Label = item.FindControl("lblStatus")

                If lblStatus.Text = "payment pending" Then
                    Dim lblName As Label = item.FindControl("lblName")
                    Dim lblID As Label = item.FindControl("lblID")
                    Dim lblCostSEOEdit As Label = item.FindControl("accSEO").FindControl("accSEOPane").FindControl("lblCostSEOEdit")
                    dbManager.insertPaymentItem(userLoggedIn.userID, "SEO - " + lblName.Text, Convert.ToInt32(lblCostSEOEdit.Text), "SEO", Convert.ToInt32(lblID.Text))
                End If
            Next
            For Each item As ListViewItem In lvConfirmBanner.Items
                Dim lblStatus As Label = item.FindControl("lblStatus")

                If lblStatus.Text = "payment pending" Then
                    Dim lblImageName As Label = item.FindControl("lblImageName")
                    Dim lblCost As Label = item.FindControl("lblCostOnly")
                    Dim lblID As Label = item.FindControl("lblID")
                    dbManager.insertPaymentItem(userLoggedIn.userID, "Banner - " + lblImageName.Text, Convert.ToInt32(lblCost.Text), "Banner", Convert.ToInt32(lblID.Text))
                End If
            Next
            Response.Redirect("Payment.aspx")
        End If

    End Sub
End Class
